package com.mail.server.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.api.OmsCompanyAddressApi;
import com.mail.dubbo.api.OmsOrderReturnApplyApi;
import com.mail.model.db.OmsCompanyAddress;
import com.mail.model.db.OmsOrderReturnApply;
import com.mail.model.dto.OrderReturnApplyDto;
import com.mail.model.vo.OrderReturnApplyVo;
import com.mail.model.vo.PageResult;
import com.mail.server.exception.BusinessException;
import com.mail.server.exception.ErrorResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class OmsOrderReturnApplyService {

    @DubboReference
    private OmsOrderReturnApplyApi orderReturnApplyApi;

    @DubboReference
    private OmsCompanyAddressApi companyAddressApi;


    //分页查询退货申请
    public PageResult list(OrderReturnApplyDto dto) {
        OmsOrderReturnApply orderReturnApply = new OmsOrderReturnApply();
        BeanUtils.copyProperties(dto, orderReturnApply);
        if (dto.getId() != null) {
            orderReturnApply.setId(dto.getId().longValue());
        }
        if (dto.getCreateTime() != null) {
            orderReturnApply.setCreateTime(LocalDate.parse(dto.getCreateTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay());
        }
        if (dto.getHandleTime() != null) {
            orderReturnApply.setHandleTime(LocalDate.parse(dto.getHandleTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay());
        }
        if (StringUtils.startsWith(dto.getReceiverKeyword(), "1")) {
            orderReturnApply.setReturnPhone(dto.getReceiverKeyword());
        } else {
            orderReturnApply.setReturnName(dto.getReceiverKeyword());
        }
        Page page = orderReturnApplyApi.pageOrderReturn(dto.getPageNum(), dto.getPageSize(), orderReturnApply);
        if (page == null) {
            throw new BusinessException(ErrorResult.error());
        }
        return new PageResult(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords());
    }

    //根据退货申请id，查询退货申请详情
    public OrderReturnApplyVo getOrderReturn(Integer id) {
        OmsOrderReturnApply returnApply = orderReturnApplyApi.findById(id.longValue());
        Long companyAddressId = returnApply.getCompanyAddressId();
        OmsCompanyAddress companyAddress = companyAddressApi.findCompanyAddressById(companyAddressId);
        return OrderReturnApplyVo.init(returnApply, companyAddress);
    }

    //修改退货申请状态
    public Integer updateStatus(Integer id, OmsOrderReturnApply orderReturnApply) {
        orderReturnApply.setId(id.longValue());
        orderReturnApply.setHandleTime(LocalDateTime.now());
        if (orderReturnApply.getStatus() == 2) {
            orderReturnApply.setReceiveTime(LocalDateTime.now());
        }
        return orderReturnApplyApi.update(orderReturnApply);
    }

    //批量删除退货申请
    public Integer delete(List<Long> ids) {
        return orderReturnApplyApi.delete(ids);
    }
}
